/**
 * 
 */
package edu.umd.clip.smoothing;

/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 * Witten-Bell smoothing
 */
public class WBSmoother extends Smoother {
	private final double backoffProb;
	private final double revTotalCount;

	public  WBSmoother(SmootherProducer producer) {
		super(producer);
		
		revTotalCount = 1.0 / totalCount;
		int N1 = 0;
		int counts[] = producer.getCounts();
		for(int i=1; i<counts.length; ++i) {
			N1 += counts[i];
		}
		backoffProb = ((double) N1) / (N1 + totalCount);
	}
	
	/* (non-Javadoc)
	 * @see edu.umd.clip.smoothing.Smoother#getBackoffProb()
	 */
	@Override
	public double getBackoffProb() {
		return backoffProb;
	}

	/* (non-Javadoc)
	 * @see edu.umd.clip.smoothing.Smoother#getProb(long)
	 */
	@Override
	public double getProb(long count) {
		if (count < minCount) return 0.0; 
		return (1.0 - backoffProb) * count * revTotalCount;
	}

}
